//
//  ZYIdentityTool.m
//  生活小助手
//
//  Created by 振亚 姜 on 14-8-9.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "ZYIdentityTool.h"
#import "XZHttpTool.h"
#import "MJExtension.h"
#import "ZYIdentityInfoResult.h"
#import "ZYIdentityRevealResult.h"
#import "ZYIdentityLoseResult.h"

/**
 203801	请输入正确的15或18位身份证
 203802	错误的身份证或无结果
 203803	身份证校验位不正确
 203804	查询失败
 */

@implementation ZYIdentityTool

+ (void)getIdentityInfoWithParameter:(id)parameter success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [parameter keyValues];
    
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/idcard/index" parameters:dictParam success:^(NSDictionary *responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 203801) {
                domain = @"请输入正确的15或18位身份证";
            } else if (errorcode == 203802) {
                domain = @"错误的身份证或无结果";
            } else if (errorcode == 203803) {
                domain = @"身份证校验位不正确";
            } else if (errorcode == 203804) {
                domain = @"查询失败";
            }
            
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            
            id resultObj = [[ZYIdentityInfoResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *ipError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(ipError);
        }
    }];
}

+ (void)getIdentityRevealWithParameter:(id)parameter success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [parameter keyValues];
    
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/idcard/leak" parameters:dictParam success:^(NSDictionary *responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 203801) {
                domain = @"请输入正确的15或18位身份证";
            } else if (errorcode == 203802) {
                domain = @"错误的身份证或无结果";
            } else if (errorcode == 203803) {
                domain = @"身份证校验位不正确";
            } else if (errorcode == 203804) {
                domain = @"查询失败";
            }
            
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            
            id resultObj = [[ZYIdentityRevealResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *ipError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(ipError);
        }
    }];
}

+ (void)getIdentityLoseWithParameter:(id)parameter success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSDictionary *dictParam = [parameter keyValues];
    
    [XZHttpTool getWithUrl:@"http://apis.juhe.cn/idcard/loss" parameters:dictParam success:^(NSDictionary *responseObj) {
        int errorcode = [responseObj[@"error_code"] intValue];
        if (errorcode) {
            NSString *domain;
            if (errorcode == 203801) {
                domain = @"请输入正确的15或18位身份证";
            } else if (errorcode == 203802) {
                domain = @"错误的身份证或无结果";
            } else if (errorcode == 203803) {
                domain = @"身份证校验位不正确";
            } else if (errorcode == 203804) {
                domain = @"查询失败";
            }
            
            NSError *ipError = [NSError errorWithDomain:domain code:errorcode userInfo:nil];
            if (failure) {
                failure(ipError);
            }
        } else {
            
            id resultObj = [[ZYIdentityLoseResult class] objectWithKeyValues:responseObj];
            if (success) {
                success(resultObj);
            }
        }
    } failure:^(NSError *error) {
        NSError *ipError = [NSError errorWithDomain:@"网络无连接" code:[error code] userInfo:error.userInfo];
        if (failure) {
            failure(ipError);
        }
    }];
}

@end
